const axios = require('axios');
const BaseIpcHandler = require('../BaseIpcHandler');

class GetPipelinesHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> GetPipelinesHandler params = `, params);
        const { invoke_from } = params;

        // 业务逻辑
        let result = null;
        try {
            // const listUrl = 'https://label-anything.cdn.bcebos.com/labelapp/pipeline/configs/index.json';
            // const response = await axios.get(listUrl);
            const response = require('../../pipeline/engine/configs/index.json');
            const pipelines = response.filter((item) => item.invoke_from.includes(invoke_from));
            // console.log('response = ', response);
            // console.log('pipelines = ', pipelines);
            result = { code: 0, msg: pipelines ? pipelines : [] };
        } catch (error) {
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        // console.log(`GetPipelinesHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = GetPipelinesHandler;
